/*
 * @Author: your name
 * @Date: 2020-05-23 17:50:30
 * @LastEditTime: 2020-05-26 18:39:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\models\admin\m_admin.js
 */ 
const Sequelize = require('sequelize')

module.exports = (sequelize, DataTypes ) => {

  class Admin extends Sequelize.Model {
      // 获取营运人员列表
      static async getOperatorList(pars){
        var [page = 1, pagesize = 12, order = null, where = null] = [pars.page, pars.pagesize, pars.order, pars.where]
        let condition = {
          attributes: ['id', 'name', 'role'],
          offset: pagesize * (page - 1),
          raw: true,
          limit: parseInt(pagesize)
        }
        if (where !== null) condition.where = where
        if (order !== null) {
          condition.order = order
        }
        var result=await this.findAndCountAll(condition);
        return result
      }

      static async updateOperator(pars){
        var result=await this.update({role:pars.role,name:pars.name},{where:{id:pars.id}});
        return result
      }

      static async deleteOperator(pars){
        var result=await this.destroy({where:{id:pars.id}});
        return result
      }
      static async addOperator(pars){
        var result=await this.create({name:pars.name,pwd:pars.pwd,role:pars.role});
        return result
      }
  }
  
  Admin.init({
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    name: { type: DataTypes.STRING(45) },
    pwd: { type: DataTypes.STRING(45) },
    role: {type: DataTypes.INTEGER(1)}
  }, {
    sequelize,
    modelName: 'admin',
    freezeTableName: true,
    timestamps: false,
    comment: "管理员用户表"
  });

  return Admin;
}